package de.tabreaderandroid.tabreader.model;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class encapsulating meta information for a tab file.
 * 
 * @author André Stannek
 * 
 */
public final class TabListItem {

	/**
	 * A string containing name/artist of the song.
	 */
	private String songName;

	/**
	 * The path of the folder containing the file.
	 */
	private String path;

	/**
	 * The actual file
	 */
	private File file;

	/**
	 * @return A string containing name/artist of the song
	 */
	public String getSongName() {
		return songName;
	}

	/**
	 * @param songName
	 *            A string containing name/artist of the song
	 */
	public void setSongName(String songName) {
		this.songName = songName;
	}

	/**
	 * @return The path of the folder containing the file
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path
	 *            The path of the folder containing the file
	 */
	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * @return The actual file
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @param file
	 *            The actual file
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * Creates a {@link TabListItem} from a {@link File} object that points to a
	 * tab file.
	 * 
	 * @param file
	 *            the {@link File} object that points to the tab file
	 * @return the created {@link TabListItem}
	 */
	public static TabListItem getFromFile(File file) {
		TabListItem result = new TabListItem();
		result.setFile(file);
		result.setPath(file.getParent());
		Matcher matcher = Pattern.compile("(.*)\\.\\w{1,3}").matcher(
				file.getName());
		if (matcher.matches()) {
			result.setSongName(matcher.group(1));
		} else {
			result.setSongName(file.getName());
		}
		return result;
	}
}
